package cn.tedu.medicalpath.role.service.impl;

import cn.tedu.medicalpath.employee.mapper.AuthorityListMapper;
import cn.tedu.medicalpath.employee.pojo.entity.AuthorityList;
import cn.tedu.medicalpath.employee.pojo.vo.AuthorityListVO;
import cn.tedu.medicalpath.role.mapper.ModuleInfoMapper;
import cn.tedu.medicalpath.role.pojo.entity.ModuleInfo;
import cn.tedu.medicalpath.role.mapper.RoleMapper;
import cn.tedu.medicalpath.role.pojo.dto.RoleQuery;
import cn.tedu.medicalpath.role.pojo.dto.RoleSaveParam;
import cn.tedu.medicalpath.role.pojo.entity.Role;
import cn.tedu.medicalpath.role.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.tedu.medicalpath.role.pojo.vo.RoleVO;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private ModuleInfoMapper moduleInfoMapper;

    @Autowired
    private AuthorityListMapper authorityListMapper;

    @Override
    public List<RoleVO> selectRole(RoleQuery roleQuery) {
        List<RoleVO> roles = roleMapper.selectRoles(roleQuery);
        for (RoleVO role : roles){
            int count = 0;
            List<AuthorityListVO> list = authorityListMapper.selectByRoleId(role.getId());
            String [] moduleRoutes = new String[list.size()];
            for (AuthorityListVO authorityListVO : list){
                moduleRoutes[count] = authorityListVO.getModuleDescription();
                count++;
            }
            role.setModuleRoute(moduleRoutes);
        }
        return roles;
    }

    @Override
    public RoleVO selectRoleById(Integer id) {
        RoleVO role = roleMapper.selectRoleById(id);
        return role;
    }

    @Override
    public void insert(RoleSaveParam roleSaveParam) {
        Role role = new Role();
        BeanUtils.copyProperties(roleSaveParam,role);
        String[] moduleRoutes = roleSaveParam.getModuleRoute();
        AuthorityList authorityList = new AuthorityList();
        if(roleSaveParam.getId() == null){
            role.setCreateTime(new java.util.Date());
            role.setUpdateTime(new Date());
            roleMapper.insertRole(role);
        }else {
            role.setUpdateTime(new Date());
            roleMapper.updateRoleById(role);
            authorityListMapper.delete(role.getId());
        }
        for (String moduleRoute : moduleRoutes){
            ModuleInfo moduleInfo = moduleInfoMapper.selectByModuleRoute(moduleRoute);
            if(moduleInfo == null){
                continue;
            }
            authorityList.setRoleId(role.getId());
            authorityList.setModuleId(moduleInfo.getId());
            authorityListMapper.insert(authorityList);
        }
    }

    @Override
    public void delete(Integer id) {
        roleMapper.deleteRoleById(id);
        authorityListMapper.delete(id);
    }
}